from kivyx.widgets import ScatterPlane
from kivyx.lib import Clock

from amaze.kivy.tile import TileW
from amaze.kivy.entities import ENTITY_WIDGET_MAP


class MazeView(ScatterPlane):
    def __init__(self, maze):
        ScatterPlane.__init__(self)
        self.object = maze
        self.scale = 20
        self.entity_to_widget = {}
        for tile in maze.iter_tiles():
            self.add_widget(TileW(tile))
        for entity in maze.all_entities:
            widget_cls = ENTITY_WIDGET_MAP[type(entity)]
            widget = widget_cls(entity)
            self.add_widget(widget)
            self.entity_to_widget[entity] = widget
        Clock.schedule_interval(self.step, 1.0 / 60.0)

    def step(self, dt):
        self.object.step(dt)
        for entity in self.object.active_entities:
            self.entity_to_widget[entity].update()
